package com.yiren.backstage.judge.thread;

import android.content.Context;

import com.yiren.backstage.config.enums.SiteConfigCodeEnum;
import com.yiren.backstage.currency.bean.VoiceBean;
import com.yiren.backstage.currency.constant.AutoJudgeConstant;
import com.yiren.backstage.currency.constant.SignalGConstant;
import com.yiren.backstage.currency.entity.GradeStandardEntity;
import com.yiren.backstage.currency.entity.ProjectEntity;
import com.yiren.backstage.currency.entity.SingleLightEntity;
import com.yiren.backstage.currency.enums.ResultStateEnum;
import com.yiren.backstage.currency.enums.TaskStateEnum;
import com.yiren.backstage.currency.utils.MessageUtil;
import com.yiren.backstage.db.dao.cache.LightCombinationCache;
import com.yiren.backstage.judge.handler.LightJudgeHandler;
import com.yiren.backstage.judge.services.light.ILightJudgeThreadService;
import com.yiren.backstage.judge.services.light.impl.LightJudgeThreadServiceImpl;
import com.yiren.backstage.player.api.IPlayerCallBack;
import com.yiren.backstage.player.api.PlayerApi;

import java.util.List;

/**
 * Time:2022/3/23 16:29
 * Author:xiaozan
 * Description:灯光评判线程
 */
public class LightJudgeThread implements Runnable {

    private Context context;
    private LightJudgeHandler lightJudgeHandler;

    /*** 当前执行灯光 */
    private ProjectEntity projectEntity;
    /*** 灯光播报内容集合 */
    private List<SingleLightEntity> singleLightEntities;
    /*** 当前灯光对象 */
    private SingleLightEntity singleLightEntity;
    /*** 开始评判时起始时间. */
    private long mStartTime;
    /*** 符合车辆状态开始时间  用于判断维持状态的起始点 */
    private long accordCarStateStartTime = 0;
    /*** 是否进入第二阶段 */
    private boolean intoTwoStage;
    /*** 灯光执行索引 */
    private int indexes = 0;
    /*** 是否已播报 */
    private boolean broadcast;
    /*** 灯光扣分是否在播报 */
    private boolean isLightGradePlay;

    private ILightJudgeThreadService iLightJudgeThreadService;

    public LightJudgeThread(Context context, LightJudgeHandler lightJudgeHandler, ProjectEntity projectEntity) {
        this.context = context;
        this.lightJudgeHandler = lightJudgeHandler;
        this.projectEntity = projectEntity;
        this.singleLightEntities = LightCombinationCache.getLightCombination(projectEntity.getId()).getSingleLightEntities();
        iLightJudgeThreadService = new LightJudgeThreadServiceImpl(context, lightJudgeHandler);
    }

    @Override
    public void run() {
        try {
            while (lightJudgeHandler.getTaskStateEnum() != TaskStateEnum.FORCT_CLOSE) {
                //灯光全部执行完成
                if (indexes > singleLightEntities.size() - 1) {
                    lightJudgeHandler.sendMessage(MessageUtil.newMessage(AutoJudgeConstant.LIGHT_PROJECT_FINISHED, projectEntity));
                    return;
                }
                singleLightEntity = singleLightEntities.get(indexes);
                //播报灯光内容
                if (!broadcast) {
                    ResultStateEnum resultStateEnum = iLightJudgeThreadService.playLightContent(singleLightEntity.getContent());
                    if (resultStateEnum == ResultStateEnum.FAIL) {
                        playGradeStandard();
                        continue;
                    }
                }
                broadcast = true;
                if (mStartTime == 0) mStartTime = System.currentTimeMillis();

                //判断灯光操作是否符合
                if (!intoTwoStage) {
                    //第一个等待阶段
                    ResultStateEnum resultStateEnum = iLightJudgeThreadService.waitOperationResult(singleLightEntity, mStartTime);
                    switch (resultStateEnum) {
                        case SUCCESS:
                            if ((boolean) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.D7.toString())) {
                                //执行下一个
                                initValue();
                                indexes++;
                            } else {
                                //进入第二个阶段
                                intoTwoStage = true;
                                accordCarStateStartTime = System.currentTimeMillis();
                            }
                            break;
                        case FAIL:
                            playGradeStandard();
                            break;
                    }
                } else {
                    //第二个维持阶段
                    ResultStateEnum resultStateEnum = iLightJudgeThreadService.keepOperationResult(singleLightEntity, accordCarStateStartTime);
                    switch (resultStateEnum) {
                        case SUCCESS:
                            //执行下一个
                            initValue();
                            indexes++;
                            break;
                        case FAIL:
                            playGradeStandard();
                            break;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 初始化
     */
    private void initValue() {
        mStartTime = 0;
        accordCarStateStartTime = 0;
        intoTwoStage = false;
        broadcast = false;
        isLightGradePlay = false;
    }

    /**
     * 播报灯光扣分
     */
    private void playGradeStandard() {
        try {
            GradeStandardEntity gradeStandardEntity = singleLightEntity.getGradeStandardEntity();
            if (null != gradeStandardEntity) {
                gradeStandardEntity.setProjectEntity(projectEntity);
                lightJudgeHandler.sendMessage(MessageUtil.newMessage(AutoJudgeConstant.ADD_PROJECT_GRADE, gradeStandardEntity));
            }
            if (null == gradeStandardEntity || !(boolean) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.D10.toString())) {
                //播报下一个
                initValue();
                indexes++;
                return;
            }
            //灯光错误后播报扣分
            String content = gradeStandardEntity.getStandardContent() + ",扣" + gradeStandardEntity.getGrade() + "分";
            isLightGradePlay = true;
            PlayerApi.playProjectAndJudgeVoice(context, new VoiceBean(content, ""), new IPlayerCallBack() {
                @Override
                public void playFinish() {
                    isLightGradePlay = false;
                }
            });
            //等待播报完成
            while (isLightGradePlay && lightJudgeHandler.getTaskStateEnum() != TaskStateEnum.FORCT_CLOSE) {
                Thread.sleep(100);
            }
            initValue();
            indexes++;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
